#include "method_filter.h"

MethodFilter::MethodFilter(std::vector<std::shared_ptr<AbcFunc>> &methods)
    : sourceMethods(methods), indices(methods.size())
{
    std::iota(indices.begin(), indices.end(), 0);
}

MethodFilter &MethodFilter::filter(std::function<bool(const std::shared_ptr<AbcFunc> &)> predicate)
{
    indices.erase(
        std::remove_if(indices.begin(), indices.end(),
                       [this, &predicate](size_t idx)
                       {
                           return !predicate(sourceMethods[idx]);
                       }),
        indices.end());
    return *this;
}

size_t MethodFilter::count() const
{
    return indices.size();
}

std::vector<std::shared_ptr<AbcFunc>> MethodFilter::getCopies() const
{
    std::vector<std::shared_ptr<AbcFunc>> result;
    result.reserve(indices.size());
    for (size_t idx : indices)
    {
        result.push_back(sourceMethods[idx]);
    }
    return result;
}